<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Lyrics</title>
	<script type="text/javascript" src="../js/lyrics.js"></script>
	<link rel="stylesheet" href="../css/lyricer.css">
	<link href="https://fonts.googleapis.com/css?family=M+PLUS+1p" rel="stylesheet">
</head>
<body style="margin: 0;">
    <img style="" id="backgroundImage" alt="Background Image"></img>
	<div style="width: 100%; height: 100%; opacity: 0.8;  z-index: 1; background-color: rgba(0, 0, 0, 0.5); position: absolute;"></div>
	<div id="lyricer">
	</div>
	<script type="text/javascript">
	const { ipcRenderer } = require('electron');

	var text = "";
	var lrcfile = "";
	var lrc = new Lyricer();
	var prevartist = '';
	var prevtrack = '';
	var prevlyrics = ['','','','','']; //track, artist, songid, lyrics, translated lyrics
    var globaltoken = '';
	function revisedRandId() {
     return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(2, 10);
    }
	/* get token */
	function getToken(mode, track, artist, songid, lang, time) {
		    var url  = "https://apic-desktop.musixmatch.com/ws/1.1/token.get?app_id=web-desktop-app-v1.0&t="+revisedRandId();
			var req = new XMLHttpRequest();  
			req.overrideMimeType("application/json");
			req.open('GET', url, true);
			req.setRequestHeader("authority", "apic-desktop.musixmatch.com");
			req.onload  = function() {	
			var jsonResponse = JSON.parse(this.responseText);
			var status2 = jsonResponse["message"]["header"]["status_code"] ;
		    if (status2 == 200){
			token = jsonResponse["message"]["body"]["user_token"] ?? '';
			if (token != "" && token != "UpgradeOnlyUpgradeOnlyUpgradeOnlyUpgradeOnly"){
				console.log('200 token');
				// token good
				globaltoken = token;
				if (mode == 1){
                    getMXMSubs(track, artist, token, lang, time);
				} else {
					getMXMTrans(songid, lang, token);
				}
			} else {
				console.log('fake 200 token');
				if (mode == 1){
					ipcRenderer.send('LyricsMXMFailed','');
			    }
		    }} else {
				console.log('token 4xx');
				if (mode == 1){
					ipcRenderer.send('LyricsMXMFailed','');
				}
	        }
		    
	    }
		req.send();
    }
	function getMXMSubs(track, artist, token, lang, time) {
		var usertoken = encodeURIComponent(token);
		var timecustom = `&f_subtitle_length=${time}&q_duration=${time}&f_subtitle_length_max_deviation=40`;
		var url  = "https://apic-desktop.musixmatch.com/ws/1.1/macro.subtitles.get?format=json&namespace=lyrics_richsynched&subtitle_format=lrc&q_artist="+artist+"&q_track="+track+"&usertoken="+usertoken+timecustom+"&app_id=web-desktop-app-v1.0&t="+revisedRandId();
		var req = new XMLHttpRequest();  
        req.overrideMimeType("application/json");
        req.open('GET', url, true);
		req.setRequestHeader("authority", "apic-desktop.musixmatch.com");
        req.onload  = function() {	
        var jsonResponse = JSON.parse(this.responseText);
		console.log(jsonResponse);
		var status1 = jsonResponse["message"]["header"]["status_code"] ;

		if (status1 == 200){    
			var id = '';
			try{
                if (jsonResponse["message"]["body"]["macro_calls"]["matcher.track.get"]["message"]["header"]["status_code"] == 200 && jsonResponse["message"]["body"]["macro_calls"]["track.subtitles.get"]["message"]["header"]["status_code"] == 200) {
					prevartist = artist;
					prevtrack = track;
					id = jsonResponse["message"]["body"]["macro_calls"]["matcher.track.get"]["message"]["body"]["track"]["track_id"] ?? '';
					lrcfile = jsonResponse["message"]["body"]["macro_calls"]["track.subtitles.get"]["message"]["body"]["subtitle_list"][0]["subtitle"]["subtitle_body"];}
				
				// /*  MXM Instrumentals detection */	
				// else if (jsonResponse["message"]["body"]["macro_calls"]["track.subtitles.get"]["message"]["header"]["instrumental"] == 1){
				// 	print('Nah');
				// 	ipcRenderer.send('LyricsHandlerNE','[00:00] Instrumental. / Lyrics not found.');
				// 	return '';
				// }

					if (lrcfile == "" ){
						// track not found 
			            console.log('track not found');
						prevlyrics = [track,artist,'','',''];
						ipcRenderer.send('LyricsMXMFailed','');
					} else {
						lrcfile = lrcfile.concat("\r\n"); 
						var timearr = lrcfile.match(/\[([0-9]+):([0-9.]+)]/g);
						timearr.unshift('[00:00.000]');			
						for (i = 0; i < timearr.length -1 ; i++){
							var rawTime = timearr[i+1].match(/(\d+:)?(\d+:)?(\d+)(\.\d+)?/);
							var hours = (rawTime[2] != null) ? (rawTime[1].replace(":", "")) : "0";
							var minutes =(rawTime[2] != null) ? (hours * 60 + rawTime[2].replace(":", "") * 1 + ":") : ((rawTime[1] != null) ? rawTime[1] : "00:");
							var seconds = (rawTime[3] != null) ? (rawTime[3]) : "00";
							var milliseconds = (rawTime[4] != null) ? (rawTime[4]) : ".000";
							var lrcTime = minutes + seconds + milliseconds;
							
							var rawTime2 = timearr[i].match(/(\d+:)?(\d+:)?(\d+)(\.\d+)?/);
							var hours2 = (rawTime2[2] != null) ? (rawTime2[1].replace(":", "")) : "0";
							var minutes2 = (rawTime2[2] != null) ? (hours2 * 60 + rawTime2[2].replace(":", "") * 1 + ":") : ((rawTime2[1] != null) ? rawTime2[1] : "00:");
							var seconds2 = (rawTime2[3] != null) ? (rawTime2[3]) : "00";
							var milliseconds2 = (rawTime2[4] != null) ? (rawTime2[4]) : ".000";
							var lrcTime2 = minutes2 + seconds2 + milliseconds2;
							
							if ( minutes.replace(":","") * 60 + seconds * 1  - minutes2.replace(":","") * 60 - seconds2 * 1  > 10) {
								
								if (minutes2 + (seconds2*1+10) + milliseconds2 != '00:10.000'){
								lrcfile = lrcfile.concat(`[${minutes2 + (seconds2*1+10) + milliseconds2}]lrcInstrumental` + "\r\n");}
								else {lrcfile = lrcfile.concat(`[00:00.00]lrcInstrumental` + "\r\n");}
							}
						}
						console.log(lrcfile);
						ipcRenderer.send('LyricsHandlerNE',lrcfile);
						if(lrcfile != null && lrcfile != ''){
							// load translation
							getMXMTrans(id, lang, token);
						} else {
						    ipcRenderer.send('LyricsMXMFailed','');
						}
		        } 
	        } catch(e) {
			console.log(e); 	
			// track not found ?? 
			console.log('track not found ??');
			ipcRenderer.send('LyricsMXMFailed','');
			}
		} else { //4xx rejected
			getToken(1, track, artist, '', lang, time);}
		}
		req.send(); 
    }
	function getMXMTrans(id, lang, token){
		if (lang != "disabled" && id != ''){
			var usertoken = encodeURIComponent(token);
			var url2  = "https://apic-desktop.musixmatch.com/ws/1.1/crowd.track.translations.get?translation_fields_set=minimal&selected_language="+lang+"&track_id="+id+"&comment_format=text&part=user&format=json&usertoken="+usertoken+"&app_id=web-desktop-app-v1.0&t="+revisedRandId();			
			var req2 = new XMLHttpRequest();  
			req2.overrideMimeType("application/json");
			req2.open('GET', url2, true);
			req2.setRequestHeader("authority", "apic-desktop.musixmatch.com");
			req2.onload  = function() {
				var jsonResponse2 = JSON.parse(this.responseText);
				console.log(jsonResponse2); 
				var status2 = jsonResponse2["message"]["header"]["status_code"] ;
				if (status2 == 200){
					try{  
					var lyrics = jsonResponse2["message"]["body"]["translations_list"];
					console.log(lyrics);
					if (lyrics.length > 0) {
						ipcRenderer.send('LyricsHandlerTranslation',lyrics);}
				    }catch(e){
				      /// not found trans -> ignore		
					}
				} else { //4xx rejected
			        getToken(2,'', '', id, lang, '');   
				}
		    }
			req2.send();
		}		
	}

	ipcRenderer.on('mxmcors', function (event, track, artist, lang, time) {
		console.log("hello");
		if (track != "" & track != "No Title Found"){
			if ( globaltoken != null && globaltoken != '' ) {
			console.log("we good");
			getMXMSubs(track, artist, token, lang, time)
			} else {
			console.log("get token");
			getToken(1,track, artist, '', lang, time);
			}
		}	
    });
	

	lrc.setLrc(text);
	</script>
</body>
</html>